
package com.hundsun.cprs.yyt.service.plate.service.impl;

import java.util.Arrays;
import java.util.List;

import com.hundsun.cprs.yyt.service.goods.domain.GoodsWithPlate;
import com.hundsun.cprs.yyt.service.goods.service.GoodsWithPlateService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hundsun.cprs.yyt.common.exception.ServiceException;
import com.hundsun.cprs.yyt.service.form.service.FormFacadeService;
import com.hundsun.cprs.yyt.service.plate.dao.PlateDao;
import com.hundsun.cprs.yyt.service.plate.domain.Plate;
import com.hundsun.cprs.yyt.service.plate.domain.PlateImage;
import com.hundsun.cprs.yyt.service.plate.domain.PlateInfo;
import com.hundsun.cprs.yyt.service.plate.domain.query.PlateQuery;
import com.hundsun.cprs.yyt.service.plate.service.PlateImageService;
import com.hundsun.cprs.yyt.service.plate.service.PlateService;

@Service
public class PlateServiceImpl implements PlateService {
    
	@Autowired
	private PlateDao plateDao;
	@Autowired
	private PlateImageService plateImageService;
	@Value("${newPlateCode}")
	private String plateCode;
	@Autowired
	private FormFacadeService formFacadeService;
	@Autowired
	private GoodsWithPlateService goodsWithPlateService;
	   
	@Override
	public void selectPlateByPage(PlateQuery query) {
		query.setIsDelete("N");
		int totalCount = plateDao.selectPlateCount(query);
		query.setTotalCount(totalCount);
		List<Plate> plateList=plateDao.selectPlateByPage(query, query.getBeginStart(),query.getPageSize());
		query.setData(plateList);
	}
	
	@Override
	@Transactional
	public void createPlate(PlateInfo plate) {
		plateDao.createPlate(plate);
		if(CollectionUtils.isNotEmpty(plate.getImageList())){
			for(PlateImage image:plate.getImageList()){
				image.setPlateId(plate.getId());
				plateImageService.insertPlateImage(image);
			}
		}
		
		if (CollectionUtils.isNotEmpty(plate.getFormAttrValueList())) {
		    formFacadeService.createFormAttrValues(plate.getFormAttrValueList(),plate.getId());
        }
	}
	@Override
	public Long selectProjectPlateId() {
		return plateDao.selectProjectPlateId();
	}
	@Override
	public Plate selectPlateById(Long id) {
		return plateDao.selectPlateById(id);
	}
	@Override
    @CacheEvict(value="PlateInfoCache:Id",key="new String(#plate.id)")
	@Transactional
	public void update(PlateInfo plate) {
		//修改专场基本信息
		plateDao.updatePlate(plate);
		//修改专场的关联图片信息
		List<PlateImage> plateImageList=plateImageService.selectPlateImageList(plate.getId());
		for (PlateImage plateImage:plateImageList){
			plateImageService.delete(plateImage);
		}
		if(CollectionUtils.isNotEmpty(plate.getImageList())){
			for(PlateImage plateImage:plate.getImageList()){
				plateImage.setPlateId(plate.getId());
				plateImageService.insertPlateImage(plateImage);
			}
		}
		
	    if (CollectionUtils.isNotEmpty(plate.getFormAttrValueList())) {
	       formFacadeService.updateFormAttrValues(plate.getFormAttrValueList());
	    }
	}
	@Override
	public List<Plate> selectPlateList(PlateQuery query) {
	    if (query != null) {
	        query.onlyReturnList();
        }
		return plateDao.selectPlateList(query);
	}
	@Override
	public List<Plate> selectTopShowList() {
		return plateDao.selectTopShowList();
	}
	@Override
	public List<Plate> selectPlateBySort(Long plateSort) {
		return plateDao.selectPlateBySort(plateSort);
	}
	@Override
	public List<Plate> selectPlateByCode(String plateCode) {
		return plateDao.selectPlateByCode(plateCode);
	}
    
    @Override
    public List<Plate> getConfigPlateList() {
        if (StringUtils.isNotEmpty(plateCode)) {
            Plate plate = plateDao.selectPlateByOneCode(plateCode);
            if (plate != null && plate.getId() != null) {
                return Arrays.asList(plate);
            }
        }
        return null;
    }
    
    @Override
    public Plate getNewPlate() {
        if (StringUtils.isNotEmpty(plateCode)) {
            Plate plate = plateDao.selectPlateByOneCode(plateCode);
            if (plate != null && plate.getId() != null) {
                return plate;
            }
        }
        return null;
    }

	@Override
	public List<Plate> getPlateList() {
		return plateDao.getPlateList();
	}

    @Override
    @Cacheable(value="PlateInfoCache:Id",key="new String(#id)")
    public PlateInfo selectPlateInfoById(Long id) {
        PlateInfo plateInfo = plateDao.selectPlateInfoById(id);
        if (plateInfo == null) {
            return null;
        }
        //加载图片
        plateInfo.setImageList(plateImageService.selectPlateImageList(plateInfo.getId()));
        //加载扩展动态属性
        //plateInfo.setFormAttrValueList(formFacadeService.selectByFormCodeAndObjectId(DYNAMIC_FORM_CODE, id));
        return plateInfo;
    }

    @Override
    @CacheEvict(value="PlateInfoCache:Id",key="new String(#plateId)")
    @Transactional
    public void deletePlate(Long plateId) {
       Plate plate =  plateDao.selectPlateById(plateId);
       if (plate == null) {
           throw new ServiceException("专场不存在");
       }
       boolean isExist = false;
       GoodsWithPlate goodsWithPlate = new GoodsWithPlate();
       goodsWithPlate.setPlateId(plateId.intValue());
       List<GoodsWithPlate> goodsWithPlateList = goodsWithPlateService.selectGoodsWithPlate(goodsWithPlate);
       if (!goodsWithPlateList.isEmpty()){
		   isExist = true;
	   }
       if(!isExist){
           //逻辑删除专场信息
           plate.setIsDelete("Y");
           plateDao.updatePlate(plate);
       }else{
           throw new ServiceException("该专场下存在已上架商品，无法删除");
       }
    }
}
